package com.otod.sys.menu.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.sys.menu.bo.MenuTreeBO;
import com.otod.sys.menu.service.IMenuService;
import com.otod.sys.vo.AuapTMenuVo;

@Controller
@RequestMapping("sys/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private IMenuService menuService;
	
	/**
	 * 查询菜单树（所有）有权限的查询  修改过名字 原名字为getAllMenu 修改为getMenu
	 * @param request
	 * @return 
	 */
	@RequestMapping(value = "getMenu", method = RequestMethod.GET)
	@ResponseBody
	public List<MenuTreeBO> getMenu(HttpServletRequest request) {
		//得到session
		String empNo = SessionUserUtil.getCurrentUser().getUserNo();
		
		//通过emp表中的 uuid 做关联查询 
		//SessionUserHolder user = SessionUserUtil.getCurrentUser();
		//return menuService.getMenuTree(user.getUserCode());
		return menuService.getMenuTree(empNo);
	}
	
	/**
	 * 查询菜单树（所有）
	 * @return 
	 */
	@RequestMapping(value = "getAllMenuList", method = RequestMethod.POST)
	@ResponseBody
	public List<AuapTMenuVo> getAllMenuList() {
		return menuService.getAllMenuTreeList();
	}
	
	/**
	 * 查询菜单树当前菜单的所有子节点
	 * @return 
	 */
	@RequestMapping(value = "getAllChildMenuList", method = RequestMethod.POST)
	@ResponseBody
	public List<AuapTMenuVo> getAllChildMenuList(String uuid) {
		return menuService.queryChildrenForList(uuid);
	}
	
	/**
	 * 查询菜单（类型为菜单）   
	 * @return 
	 */
	@RequestMapping(value = "getMenuList", method = RequestMethod.POST)
	@ResponseBody
	public List<AuapTMenuVo> getMenuList() {
		return menuService.getAllMenuList();
	}
	
	/**
	 * 通过菜单编号查询菜单详细信息
	 * @param id
	 * @param request
	 * @return 
	 */
	@RequestMapping(value = "getMenuById", method = RequestMethod.POST)
	@ResponseBody
	public AuapTMenuVo getMenuById(@RequestBody String menuId,HttpServletRequest request) {
		return menuService.getMenuById(menuId);
	}
	
	/**
	 * 保存
	 * @throws AppException 
	 */
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(AuapTMenuVo auapTMenu,HttpServletRequest request) throws AppException{
		if(StringUtils.isEmpty(auapTMenu.getUuid())){
			menuService.insert(auapTMenu);
		}else{
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("uuid", auapTMenu.getUuid());
			map.put("menuName", auapTMenu.getMenuName());
			map.put("menuType", auapTMenu.getMenuType());
			map.put("menuUri", auapTMenu.getMenuUri());
			map.put("menuIco", auapTMenu.getMenuIco());
			map.put("menuOrder", auapTMenu.getMenuOrder());
			map.put("isShow", auapTMenu.getIsShow());
			map.put("menuTrip", auapTMenu.getMenuTrip());
			map.put("parentMenuId", auapTMenu.getParentMenuId());
			map.put("menuPath", auapTMenu.getMenuPath());
			menuService.update(map);
		}
		
		return JsonSuccess.factory("菜单保存成功！");
	}
	
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@RequestMapping(value = "deleteById", method = RequestMethod.POST)
	@ResponseBody
	public String deleteById(@RequestBody String menuId) throws AppException{
		menuService.deleteById(menuId);
		
		return JsonSuccess.factory("菜单删除成功！");
	}
}
